﻿using System;
using System.Collections.Generic;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;
using ZLDC.Topo.Model.Estate;
using ZLDC.DAL.Estate;

namespace ZLDC.Topo.BLL.Estate
{
    public class HousingBLL
    {
                                public static string p_housing_save(int Id, int dId, string dname, int aId, string area, string buildName, int buildId, string roomNo, int roomId, int tradeType, int room, int hall, int restroom, int balcony, int kitchen, float acreage, float totalprice, float leaseprice, float unitprice, string owner, string ownerTel, string linkman, string linkmanTel, float costprice, string source, string pocNo, string pocStatus, int redeem, string useStatus, string propertyRight, string direct, string fitment, string houseStyle, int use, string houseType, int hasKey, string keyNo, string keepKey, string keeyKeyDate, int ctyCont, string ctyContUser, string ctyContDate, int isOnly, string OnlyUser, string OnlyDate, int markUId, string markUser, int markUDeptId, string markUDept, string remark, string tenant, float assignmentfee, int addUId, string addUser, string addUDept, int addUDeptId, string addUBank, int Protected, int clickCount, string hAppliances, string mark1, string mark2, string mark3, string mark4, string mark5, int status, int status1, string modifyUser, int dltFlag, int isValid, int recommend, int hasPhoto, int buildId1, int buildId2, int buildId3, string EndDate, string ctyEndDate)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingDAL.p_housing_save(Id, dId, dname, aId, area, buildName, buildId, roomNo, roomId, tradeType, room, hall, restroom, balcony, kitchen, acreage, totalprice, leaseprice, unitprice, owner, ownerTel, linkman, linkmanTel, costprice, source, pocNo, pocStatus, redeem, useStatus, propertyRight, direct, fitment, houseStyle, use, houseType, hasKey, keyNo, keepKey, keeyKeyDate, ctyCont, ctyContUser, ctyContDate, isOnly, OnlyUser, OnlyDate, markUId, markUser, markUDeptId, markUDept, remark, tenant, assignmentfee, addUId, addUser, addUDept, addUDeptId, addUBank, Protected, clickCount, hAppliances, mark1, mark2, mark3, mark4, mark5, status, status1, modifyUser, dltFlag, isValid, recommend, hasPhoto, buildId1, buildId2, buildId3, EndDate, ctyEndDate, out  outmsg, out  newid, db);
            }

            return outmsg;
        }

                                public static string p_changehousing_save(int Id, int dId, string dname, int aId, string area, string buildName, int buildId, string roomNo, int roomId, int tradeType, int room, int hall, int restroom, int balcony, int kitchen, float acreage, float totalprice, float leaseprice, float unitprice, string owner, string ownerTel, string linkman, string linkmanTel, float costprice, string source, string pocNo, string pocStatus, int redeem, string useStatus, string propertyRight, string direct, string fitment, string houseStyle, int use, string houseType, int hasKey, string keyNo, string keepKey, string keeyKeyDate, int ctyCont, string ctyContUser, string ctyContDate, int isOnly, string OnlyUser, string OnlyDate, int markUId, string markUser, int markUDeptId, string markUDept, string remark, string tenant, float assignmentfee, int addUId, string addUser, string addUDept, int addUDeptId, string addUBank, int Protected, int clickCount, string hAppliances, string mark1, string mark2, string mark3, string mark4, string mark5, int status, int status1, string modifyUser, int dltFlag, int isValid, int recommend, int hasPhoto, int buildId1, int buildId2, int buildId3,string EndDate,string ctyEndDate)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingDAL.p_changehousing_save(Id, dId, dname, aId, area, buildName, buildId, roomNo, roomId, tradeType, room, hall, restroom, balcony, kitchen, acreage, totalprice, leaseprice, unitprice, owner, ownerTel, linkman, linkmanTel, costprice, source, pocNo, pocStatus, redeem, useStatus, propertyRight, direct, fitment, houseStyle, use, houseType, hasKey, keyNo, keepKey, keeyKeyDate, ctyCont, ctyContUser, ctyContDate, isOnly, OnlyUser, OnlyDate, markUId, markUser, markUDeptId, markUDept, remark, tenant, assignmentfee, addUId, addUser, addUDept, addUDeptId, addUBank, Protected, clickCount, hAppliances, mark1, mark2, mark3, mark4, mark5, status, status1, modifyUser, dltFlag, isValid, recommend, hasPhoto, buildId1, buildId2, buildId3,EndDate,ctyEndDate, out  outmsg, out  newid, db);
            }

            return outmsg;
        }

                                                                        public static string p_housing_list(int userid, int page, int pagesize, string condition)
        {
            string orderByName = "";
            string orderByType = "";

            return p_housing_list(userid, page, pagesize, condition, orderByName, orderByType);
        }

                                                                public static string p_housing_list(int userid, int page, int pagesize, string condition, string orderByName, string orderByType)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_list_table(userid, page, pagesize, condition, orderByName, orderByType, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);               
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                                public static string p_housing_list_action(int userid, int page, int pagesize, string condition, string orderByName, string orderByType)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_list_action_table(userid, page, pagesize, condition, orderByName, orderByType, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
                        string field = UserOptionBLL.p_user_option_detail(1, userid);

            _Json json = new _Json(dt);
            return "{" + field + ",page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                                public static string p_housing_list_check(int userid, int page, int pagesize, string condition, string orderByName, string orderByType)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_list_check_table(userid, page, pagesize, condition, orderByName, orderByType, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
                        string field = UserOptionBLL.p_user_option_detail(1, userid);

            _Json json = new _Json(dt);
            return "{" + field + ",page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                                public static string p_housing_list_new(int userid, int page, int pagesize, string condition, string orderByName, string orderByType)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_list_new_table(userid, page, pagesize, condition, orderByName, orderByType, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
                        string field = UserOptionBLL.p_user_option_detail(1, userid);

            _Json json = new _Json(dt);
            return "{" + field + ",page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                                public static string p_housing_list_favorite(int userid, int page, int pagesize, string condition, string orderByName, string orderByType)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_list_favorite_table(page, pagesize, condition, orderByName, orderByType, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
                        string field = UserOptionBLL.p_user_option_detail(1, userid);

            _Json json = new _Json(dt);
            return "{" + field + ",page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                                public static string p_housing_list_my(int userid, int page, int pagesize, string condition, string orderByName, string orderByType)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_list_my_table(userid, page, pagesize, condition, orderByName, orderByType, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
                        string field = UserOptionBLL.p_user_option_detail(1, userid);

            _Json json = new _Json(dt);
            return "{" + field + ",page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                public static string HousingDelete(int id,int roomId,int tradeType)
        {
            string outmsg = "删除失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.HousingDelete(id, roomId, tradeType, db); 
            }

            if (newid > 0)
                outmsg = "删除成功";

            return outmsg;
        }

                                                public static string HousingRecommend(int id, int isrecommend)
        {
            string outmsg = "推荐失败";
            if (isrecommend == 0)
                outmsg = "取消推荐失败";

            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.HousingRecommend(id, isrecommend, db); 
            }

            if (newid > 0)
                outmsg = "推荐成功";

            if (isrecommend == 0)
                outmsg = "取消推荐成功";

            return outmsg;
        }        

                                                public static string HousingCheck(int id, int status)
        {
            string outmsg = "审核失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.HousingCheck(id, status, db); 
            }
            
                        ZLDC.Common.BaseUser.Param param = _Utility.GetInfoBySession();
            int deptid = _Convert.ToInt(param.m_sDeptID, 0);
            string deptname = param.m_sDeptName;
            int userid = _Convert.ToInt(param.m_sUserID, 0);
            string username = param.m_sUserName;
            
                        if (status == 0)
                HousingFollowBLL.p_housingFollow_save(0, id, "上盘审核", deptid, deptname, userid, username, "审核有效", 0, "");
            else
                HousingFollowBLL.p_housingFollow_save(0, id, "上盘审核", deptid, deptname, userid, username, "审核无效", 0, "");


            if (newid > 0)
            {
                
                if (status == 0)                {
                                        HousingInfo info = new HousingInfo();
                    using (_Database db = new _Database("estate"))
                    {
                        info = ZLDC.DAL.Estate.HousingDAL.p_housing_detail(id, db);
                    }
                    int isadd = 0;
                    int isadd1 = 0;
                    if (info.TradeType == 1)
                    {
                        isadd = 1;
                    }
                    else if (info.TradeType == 2)
                    {
                        isadd1 = 1;
                    }
                    quantificationBLL.UpdateHousingQuantification(id, info.MarkUId, info.MarkUDeptId, info.MarkUDept, info.MarkUser, isadd, 0, 0, 0, 0, isadd1);
                                        quantificationBLL.UpdateHousingQuantificationByUserName(id, info.HasKey, info.IsOnly, info.CtyCont, info.KeepKey, info.OnlyUser, info.CtyContUser);

                   





                }
                outmsg = "审核成功";
            }

            return outmsg;
        }


                                                        public static string HousingCheckStatus1(int id, int status1)
        {
            string outmsg = "";

                        ZLDC.Common.BaseUser.Param param = _Utility.GetInfoBySession();
            int deptid = _Convert.ToInt(param.m_sDeptID, 0);
            string deptname = param.m_sDeptName;
            int userid = _Convert.ToInt(param.m_sUserID, 0);
            string username = param.m_sUserName;

                        HousingInfo info = HousingBLL.p_housing_detail(id);
            if (info.Status1 == 2)
                outmsg = "资料盘->当前盘";
            else if (info.Status1 == 3)
                outmsg = "无效盘->当前盘";

            if (status1 == 1)
                outmsg += "(通过)";
            else if (status1 == 2)
                outmsg += "(不通过)";

            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.HousingCheckStatus1(id, status1, db);
            }

                        HousingFollowBLL.p_housingFollow_save(0, id, "转盘", deptid, deptname, userid, username, outmsg, 0, "");

                        if (status1 == 1)
            {
                int isadd = 0;
                int isadd1 = 0;
                if (info.TradeType == 1)
                {
                    isadd = 1;
                }
                else if (info.TradeType == 2)
                {
                    isadd1 = 1;
                }
                quantificationBLL.UpdateHousingQuantification(id, info.MarkUId, info.MarkUDeptId, info.MarkUDept, info.MarkUser, isadd, 0, 0, 0, 0, isadd1);

            }

            return outmsg;
        }

                                                        public static string HousingCheckStatus2(int id, int status1)
        {
            string outmsg = "";

                        ZLDC.Common.BaseUser.Param param = _Utility.GetInfoBySession();
            int deptid = _Convert.ToInt(param.m_sDeptID, 0);
            string deptname = param.m_sDeptName;
            int userid = _Convert.ToInt(param.m_sUserID, 0);
            string username = param.m_sUserName;

                        HousingInfo info = HousingBLL.p_housing_detail(id);
            if (info.Status1 == 2)
                outmsg = "资料盘->当前盘";
            else if (info.Status1 == 3)
                outmsg = "无效盘->当前盘";

            if (status1 == 1)
                outmsg += "(通过)";
            else if (status1 == 2)
                outmsg += "(不通过)";

            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.HousingCheckStatus1(id, status1, db);
            }

                        if (status1 == 1)
            {
                int isadd = 0;
                int isadd1 = 0;
                if (info.TradeType == 1)
                {
                    isadd = 1;
                }
                else if (info.TradeType == 2)
                {
                    isadd1 = 1;
                }
                quantificationBLL.UpdateHousingQuantification(id, info.MarkUId, info.MarkUDeptId, info.MarkUDept, info.MarkUser, isadd, 0, 0, 0, 0, isadd1);

            }

            return outmsg;
        }  

                                                public static string p_housing_detail_table(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_detail_table(id, db); 
            }

            _Json json = new _Json(dt, "");
            return "{record:" + json.ToJson() + "}";
        }

                                                public static HousingInfo p_housing_detail(int id)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.HousingDAL.p_housing_detail(id, db);
            }
        }

                                                        public static string HousingLock(int id, int isProtected)
        {
            string outmsg = "封盘失败";
            if (isProtected == 0)
                outmsg = "提前解封失败";

            int newid = 0;

            int protectedUid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string protectedUser = _Utility.GetInfoBySession().m_sUserName;
            int protectedUDeptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            string protectedUDept = _Utility.GetInfoBySession().m_sDeptName;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.HousingLock(id, isProtected, protectedUid, protectedUser, protectedUDeptId, protectedUDept, db);
            }

            if (newid > 0)
                outmsg = "封盘成功";

            if (isProtected == 0)
                outmsg = "提前解封成功";

                        HousingFollowBLL.p_housingFollow_save(0, id, "封盘", protectedUDeptId, protectedUDept, protectedUid, protectedUser, outmsg, 0, "");

            return outmsg;
        }

                                                        public static string HousingStatus(int id, int status, string remark)
        {
            string outmsg = "";

                        ZLDC.Common.BaseUser.Param param = _Utility.GetInfoBySession();
            int deptid = _Convert.ToInt(param.m_sDeptID, 0);
            string deptname = param.m_sDeptName;
            int userid = _Convert.ToInt(param.m_sUserID, 0);
            string username = param.m_sUserName;

                        HousingInfo info = HousingBLL.p_housing_detail(id);
            if(info.Status == 1)
                outmsg = "当前盘->";
            else if(info.Status == 2)
                outmsg = "资料盘->";
            else if(info.Status == 3)
                outmsg = "无效盘->";

            using (_Database db = new _Database("estate"))
            {
                                string temp_outmsg = "";
                if (status == 1)
                {
                    outmsg += "当前盘(申请)";

                                        if (info.Status == 2)
                    {
                                                ZLDC.DAL.Estate.HousingDAL.p_housing_change(id, 2, userid, username, deptid, deptname, outmsg, remark, out temp_outmsg, db);                    
                    }
                    else if (info.Status == 3)
                    {
                        ZLDC.DAL.Estate.HousingDAL.p_housing_change(id, 3, userid, username, deptid, deptname, outmsg, remark, out temp_outmsg, db);
                    }
                    
                                        if (temp_outmsg != "")
                        outmsg = temp_outmsg;       
                }
                else if (status == 2)
                {
                    outmsg += "资料盘(成功)";

                    ZLDC.DAL.Estate.HousingDAL.HousingStatus(id, 2, db);

                                        HousingFollowBLL.p_housingFollow_save(0, id, "转盘", deptid, deptname, userid, username, outmsg, 0, remark); 
                }
                else if (status == 3)
                {
                    outmsg += "无效盘(成功)";

                    ZLDC.DAL.Estate.HousingDAL.HousingStatus(id, 3, db);

                                        HousingFollowBLL.p_housingFollow_save(0, id, "转盘", deptid, deptname, userid, username, outmsg, 0, remark); 
                }
            }

            return outmsg;
        }


                                public static string p_housing_isupdatemark(int id)
        {
            string outmsg = "";

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingDAL.p_housing_isupdatemark(id, out outmsg, db);
            }

            return outmsg;
        }

                                                public static string HousingStatusPty(int id, int markUid, string markUser, int markUdeptid, string markUdept)
        {
            string outmsg = "";

                        ZLDC.Common.BaseUser.Param param = _Utility.GetInfoBySession();
            int deptid = _Convert.ToInt(param.m_sDeptID, 0);
            string deptname = param.m_sDeptName;
            int userid = _Convert.ToInt(param.m_sUserID, 0);
            string username = param.m_sUserName;

                        HousingInfo info = HousingBLL.p_housing_detail(id);
            if (info.Status == 1)
                outmsg = "当前盘->";
            else if (info.Status == 2)
                outmsg = "资料盘->";
            else if (info.Status == 3)
                outmsg = "无效盘->";

            using (_Database db = new _Database("estate"))
            {
                outmsg += "当前盘(成功),开盘人:" + markUser + ",部门:" + markUdept;

                ZLDC.DAL.Estate.HousingDAL.HousingStatus(id, markUid, markUser, markUdeptid, markUdept, db);
                                HousingFollowBLL.p_housingFollow_save(0, id, "转盘", deptid, deptname, userid, username, outmsg, 0, "");
            }

            return outmsg;
        }

                public static string SelectHousigById(int id) 
        {
            DataTable dt = null;
            dt = ZLDC.DAL.Estate.HousingDAL.SelectHousigById(id);
            _Json json = new _Json(dt, 1);
            return "{record:" + json.ToJson() + "}";
                
        }

                public static DataSet SelectBuildNameById(int id) 
        {
            return ZLDC.DAL.Estate.HousingDAL.SelectBuildNameById(id);
        }

        public static DataSet SelectBuildNameById(int id,string FiledList)
        {
            return ZLDC.DAL.Estate.HousingDAL.SelectBuildNameById(id,FiledList);
        }

                public static int SelectBuildIdByHid(int id)
        {
            DataTable dt=new DataTable ();
            int ID = 0;
            using (_Database db = new _Database("estate")) 
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_estate_buildid(id, db);
            }
            if (dt.Rows.Count > 0) 
            {
              ID= Convert.ToInt32(dt.Rows[0][0]);
            }
            return ID;

        }

                                                public static string GetAreaAndUseByDeptId(int deptid)
        {
            string condition = "";
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptid);

            if (info == null || info.ID <= 0)
                return "(aid =0)";

                        string areas = "";
            string lastChar = "";
            if (info.Areas != "")
            {
                                lastChar = info.Areas.Substring(info.Areas.Length - 1, 1);
                if (lastChar.Contains(","))
                    areas = info.Areas.Substring(0, info.Areas.Length - 1);
                else
                    areas = info.Areas;                
            }

                        string estates = "";
            if (info.Estates != "")
            {
                                lastChar = info.Estates.Substring(info.Estates.Length - 1, 1);
                if (lastChar.Contains(","))
                    estates = info.Estates.Substring(0, info.Estates.Length - 1);
                else
                    estates = info.Estates;                 
            }

                        string usage = "";
            if (info.Useage != "")
            {
                                lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                if (lastChar.Contains(","))
                    usage = info.Useage.Substring(0, info.Useage.Length - 1);
                else
                    usage = info.Useage;
            }

            if (!areas.Equals(""))
                condition += " (aid in(" + areas + ")";

            if (condition != "")
            {
                if (!estates.Equals(""))
                    condition += " or eid in(" + estates + "))";
                else
                    condition += ")";
            }
            else
            {
                if (!estates.Equals(""))
                    condition += " eid in(" + estates + ")";
            }

                        if (condition != "")
            {
                if (!usage.Equals(""))
                    condition += " and [use] in(" + usage + ")";
            }
            else
            {
                if (!usage.Equals(""))
                    condition += " [use] in(" + usage + ")";
            }

            return condition;
        }

                                                public static string GetAreaByDeptId(int deptid)
        {
            string condition = "";
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptid);

            if (info == null || info.ID <= 0)
                return "";

            string areas = "";
            string lastChar = "";
            if (info.Areas != "")
            {
                                lastChar = info.Areas.Substring(info.Areas.Length - 1, 1);
                if (lastChar.Contains(","))
                    areas = info.Areas.Substring(0, info.Areas.Length - 1);
                else
                    areas = info.Areas;
            }

                        string estates = "";
            if (info.Estates != "")
            {
                                lastChar = info.Estates.Substring(info.Estates.Length - 1, 1);
                if (lastChar.Contains(","))
                    estates = info.Estates.Substring(0, info.Estates.Length - 1);
                else
                    estates = info.Estates;
            }

            string usage = "";
            if (info.Useage != "")
            {
                                lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                if (lastChar.Contains(","))
                    usage = info.Useage.Substring(0, info.Useage.Length - 1);
                else
                    usage = info.Useage;
            }

            if (!areas.Equals(""))
                condition += " (areaid in(" + areas + ")";

            if (condition != "")
            {
                if (!estates.Equals(""))
                    condition += " or id in(" + estates + "))";
                else
                    condition += ")";
            }
            else
            {
                if (!estates.Equals(""))
                    condition += " id in(" + estates + ")";
            }

                        if (condition != "")
            {
                if (!usage.Equals(""))
                    condition += " and CHARINDEX([usage], '" + usage + "') > 0";
            }
            else
            {
                if (!usage.Equals(""))
                    condition += " CHARINDEX([usage], '" + usage + "') > 0";
            }

            return condition;
        }

                                                public static string GetNoAreaByDeptId(int deptid)
        {
            string condition = "";
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptid);

            if (info == null || info.ID <= 0)
                return "";

            string areas = "";
            string lastChar = "";
            if (info.Areas != "")
            {
                                lastChar = info.Areas.Substring(info.Areas.Length - 1, 1);
                if (lastChar.Contains(","))
                    areas = info.Areas.Substring(0, info.Areas.Length - 1);
                else
                    areas = info.Areas;
            }

                        string estates = "";
            if (info.Estates != "")
            {
                                lastChar = info.Estates.Substring(info.Estates.Length - 1, 1);
                if (lastChar.Contains(","))
                    estates = info.Estates.Substring(0, info.Estates.Length - 1);
                else
                    estates = info.Estates;
            }

            string usage = "";
            if (info.Useage != "")
            {
                                lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                if (lastChar.Contains(","))
                    usage = info.Useage.Substring(0, info.Useage.Length - 1);
                else
                    usage = info.Useage;
            }

            if (!areas.Equals(""))
                condition += " (areaid not in(" + areas + ")";

            if (condition != "")
            {
                if (!estates.Equals(""))
                    condition += " or id not in(" + estates + "))";
                else
                    condition += ")";
            }
            else
            {
                if (!estates.Equals(""))
                    condition += " id not in(" + estates + ")";
            }

            return condition;
        }

                                                public static string GetAreaByDeptId2(int deptid)
        {
            string condition = "";
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptid);

            if (info == null)
                return "";

            string areas = "";
            string lastChar = "";
            if (info.Areas != "")
            {
                                lastChar = info.Areas.Substring(info.Areas.Length - 1, 1);
                if (lastChar.Contains(","))
                    areas = info.Areas.Substring(0, info.Areas.Length - 1);
                else
                    areas = info.Areas;
            }

                        string estates = "";
            if (info.Estates != "")
            {
                                lastChar = info.Estates.Substring(info.Estates.Length - 1, 1);
                if (lastChar.Contains(","))
                    estates = info.Estates.Substring(0, info.Estates.Length - 1);
                else
                    estates = info.Estates;
            }

            string usage = "";
            if (info.Useage != "")
            {
                                lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                if (lastChar.Contains(","))
                    usage = info.Useage.Substring(0, info.Useage.Length - 1);
                else
                    usage = info.Useage;
            }

            if (!areas.Equals(""))
                condition += " areaid in(" + areas + ")";
           
            return condition;
        }

                                                                public static string p_v_LookTelLog_list_table(int page, int pagesize, string condition) 
        {
            DataTable dt = null;
            int pageall = 0;
            int total = 0;
            using (_Database db = new _Database("estate")) 
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_v_LookTelLog_list_table(page, pagesize, condition,ref total,db);
            }
            pageall = _Utility.GetPageAll(pagesize, total);                  _Json json = new _Json(dt, "yyyy-MM-dd HH:mm:ss");
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";            
        }

        public static string SelectMyHousing(int size, string condition)
        {
            string fields = "top " + size + " id,ename, mark1, buildName,roomNo,room, hall,acreage,totalprice,leaseprice,tradeType";
                        string orderby = " ";
            DataTable dt = new DataTable();
            using (_Database db = new _Database("estate"))
            {
                DataSet ds = ZLDC.DAL.Estate.HousingDAL.SelectHousing(condition,fields, orderby, db);
                dt = ds.Tables[0];
            }
            _Json json = new _Json(dt);
            return "{ record:" + json.ToJson() + "}";
        }

                                                                                public static int UpdateMark(int id, int markUid, string markUser, int markUdeptid, string markUdept)
        {
            int newid = 0;
          
            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.UpdateMark(id, markUid, markUser, markUdeptid, markUdept, db);
            }

            return newid;
        }
        
                public static int UpdateStatus(string id,int deptid_from,string deptname_from,int userid_from,string username_from,string flag,int userid_to,int deptid_to,string deptname_to,string username_to) 
        {
            int rows = 0;
            using (_Database db = new _Database("estate")) 
            {
                rows = ZLDC.DAL.Estate.HousingDAL.UpdateStatus(id,deptid_from,deptname_from,userid_from,username_from,flag,userid_to,deptid_to,deptname_to,username_to,db);
            }
            return rows;
        }
        
                public static string p_housing_Recycle_list_table(int page,int pagesize,string condition) 
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_housing_Recycle_list_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";

        }
        
        public static string DeleteHousing(int id) 
        {
            string outmsg = "删除失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.DeleteHousing(id, db);
            }

            if (newid > 0)
                outmsg = "删除成功";

            return outmsg;
        }

        public static string UpdateDltFlag(int id)
        {
            string outmsg = "恢复失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.HousingDAL.UpdateDltFlag(id, db);
            }

            if (newid > 0)
                outmsg = "恢复成功";

            return outmsg;
        }
       
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                public static int UpdateFileds(DataSet ds, int Id, int dId, string dname, int aId, string area, string buildName, int buildId, string roomNo, int roomId, int tradeType, int room, int hall, int restroom, int balcony, int kitchen, float acreage, float totalprice, float leaseprice, float unitprice, string owner, string ownerTel, string linkman, string linkmanTel, float costprice, string source, string pocNo, string pocStatus, int redeem, string useStatus, string propertyRight, string direct, string fitment, string houseStyle, int use, string houseType, int hasKey, string keyNo, string keepKey, string keepKeyDate, int ctyCont, string ctyContUser, string ctyContDate, int isOnly, string OnlyUser, string OnlyDate, int markUId, string markUser, int markUDeptId, string markUDept, string remark, string tenant, float assignmentfee, int addUId, string addUser, string addUDept, int addUDeptId, string addUBank, int Protected, int clickCount, string hAppliances, string mark1, string mark2, string mark3, string mark4, string mark5, int status, int status1, string modifyUser, int dltFlag, int isValid, int recommend, int hasPhoto, string endDate, string ctyEndDate)
        {
            int rows = 0;
            using (_Database db = new _Database("estate")) 
            {
                rows = ZLDC.DAL.Estate.HousingDAL.UpdateFileds(ds, Id, dId, dname, aId, area, buildName, buildId, roomNo, roomId, tradeType, room, hall, restroom, balcony, kitchen, acreage, totalprice, leaseprice, unitprice, owner, ownerTel, linkman, linkmanTel, costprice, source, pocNo, pocStatus, redeem, useStatus, propertyRight, direct, fitment, houseStyle, use, houseType, hasKey, keyNo, keepKey, keepKeyDate, ctyCont, ctyContUser, ctyContDate, isOnly, OnlyUser, OnlyDate, markUId, markUser, markUDeptId, markUDept, remark, tenant, assignmentfee, addUId, addUser, addUDept, addUDeptId, addUBank, Protected, clickCount, hAppliances, mark1, mark2, mark3, mark4, mark5, status, status1, modifyUser, dltFlag, isValid, recommend, hasPhoto,endDate,ctyEndDate, db);
            }
            return rows;
        }
        
                public static int UpdateKeys(int id, int hasKey, string keyNo, string keepKey, string keepKeyDate, int ctyCont, string ctyContUser, string ctyContDate, int isOnly, string OnlyUser, string OnlyDate)
        {
            int rows = 0;
            using (_Database db = new _Database("estate"))
            {
                rows= ZLDC.DAL.Estate.HousingDAL.UpdateKeys(id, hasKey, keyNo, keepKey, keepKeyDate, ctyCont, ctyContUser, ctyContDate, isOnly, OnlyUser, OnlyDate,db);
            }
            return rows;
        }

                public static int UpdateKeys(int id, int hasKey, string keyNo, string keepKey, string keepKeyDate)
        {
            int rows = 0;
            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.HousingDAL.UpdateKeys(id, hasKey, keyNo, keepKey, keepKeyDate, db);
            }
            return rows;
        }

                                                        public static string p_transaction_detail_table(int pty_id, int kty_id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_transaction_detail_table(pty_id, kty_id, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

                public static string TranToDeal(int Housingid, string DealPrice, string DealDate, string sInfo)
        {
            using (_Database db = new _Database("estate")) 
            {
               return  ZLDC.DAL.Estate.HousingDAL.TranToDeal(Housingid, DealPrice, DealDate, sInfo, db);
            }
        }

                public static string UpdateStatus1(int housingID)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.HousingDAL.UpdateStatus1(housingID,db);
            }
        }

                public static void UpdateMark5(int housingId, string importContent)
        {
            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingDAL.UpdateMark5(housingId, importContent, db);
            }
        }

                public static void UpdateMark4(int housingId, string importContent)
        {
            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingDAL.UpdateMark4(housingId, importContent, db);
            }
        }

                                                public static string ShowOtherHousing(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.ShowOtherHousing(id, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }


                                                public static string P_t_keyorExclusive_list(string userid) 
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate")) 
            {
                dt = ZLDC.DAL.Estate.HousingDAL.P_t_keyorExclusive_list(userid, db);
            }
            _Json json = new _Json(dt);

             return "{record:" + json.ToJson() + "}";

        }
                                                                public static void KeyorExclusiveSave(int hid, int userid, string username, int type)
        {
            using (_Database db = new _Database("estate")) 
            {
                ZLDC.DAL.Estate.HousingDAL.KeyorExclusiveSave(hid, userid, username, type, db);
            }
        }

                                        public static string p_CommendHousing_list()
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingDAL.p_CommendHousing_list(db);
            }
            _Json json = new _Json(dt);

            return "{record:" + json.ToJson() + "}";
        }

    }
}
